package com.iampzh.oa.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.iampzh.oa.dao.DepartmentDaoImpl;
import com.iampzh.oa.entitys.Department;

@Controller
@RequestMapping("department")
public class DepartmentController {
	@Autowired
	private DepartmentDaoImpl departmentDaoImpl;

	/**
	 * 显示所有的部门信息
	 * 
	 * @return
	 */
	@RequestMapping("list")
	public String listall(Model model) {
		List<Department> allDepartment = departmentDaoImpl.getAllDepartment();
		model.addAttribute("allDepartment", allDepartment);
		return "department/list";
	}

	/**
	 * 显示新建部门页面
	 * 
	 * @return
	 */
	@RequestMapping("departmentAction_addUI")
	public String addUI() {
		return "department/add";
	}

	/**
	 * 新增部门
	 * 
	 * @param request
	 * @return
	 */
	@PostMapping("departmentAction_add.action")
	public String addDepartment(HttpServletRequest request) {
		String dname = request.getParameter("dname");
		String ddesc = request.getParameter("ddesc");
		Department department = new Department();
		department.setDname(dname);
		department.setDdesc(ddesc);
		departmentDaoImpl.save(department);
		return "redirect:list";
	}

	/**
	 * 显示修改页面
	 * 
	 * @return
	 */
	@RequestMapping("departmentAction_editUI/{id}")
	public String editUI(@PathVariable("id") long id, Model model) {
		Department department = departmentDaoImpl.getDepartment((int) id);
		model.addAttribute("department", department);
		return "department/editUI";
	}

	@RequestMapping("departmentAction_editUI/departmentAction_edit.action")
	public String editDepartment(HttpServletRequest request) {
		String dname = request.getParameter("dname");
		String did = request.getParameter("id");
		String ddesc = request.getParameter("ddesc");
		Department department = new Department();
		department.setDid(Long.parseLong(did));
		department.setDname(dname);
		department.setDdesc(ddesc);
		departmentDaoImpl.save(department);
		return "redirect:../list";
	}
	@RequestMapping("departmentAction_delete/{id}")
	public String deleteDepartment(@PathVariable("id") int id){
		departmentDaoImpl.delete(id);
		return "redirect:../list";}
}
